## -*- coding: utf-8 -*-

#   Copyright (C) 2007, 2008 Yves Junqueira (yves@cetico.org) 
#
#    This file is part of GeekDNS.
#
#    GeekDNS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    GeekDNS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with GeekDNS.  If not, see <http://www.gnu.org/licenses/>.


"""Define novas funcoes para uso nos templates
"""

import turbogears
from turbogears.i18n.format import format_currency
from cherrypy import session, request
import unicodedata

def real(number):
  """Converts a float number to a string expressed in R$ (reais)
  """
  return "R$ " + str(format_currency(number, "pt_BR"))

def has_order():
  """Returns True if the user has started an order, unless the
  current page is /finalizapedido
  
  """
  if request.path != turbogears.url('/finalizapedido'):
    order_started = session.has_key("plan")
    return order_started
  else:
    return False

def unaccent(orig_string):
  return unicodedata.normalize("NFKD",
  orig_string).encode("ascii", "ignore" )

def add_custom_stdvars(vars):
  return vars.update({"real": real,
    "has_order" : has_order,
    "unaccent" : unaccent
    })

turbogears.view.variable_providers.append(add_custom_stdvars)
